from http.server import BaseHTTPRequestHandler, HTTPServer
import time

class StreamingHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/event-stream')
        self.send_header('Cache-Control', 'no-cache')
        self.send_header('Connection', 'keep-alive')
        self.end_headers()
        
        for i in range(10):  # 示例：发送10个事件
            message = f"data: Message {i}\n\n"
            self.wfile.write(message.encode('utf-8'))
            self.wfile.flush()  # 确保立即发送
            time.sleep(1)  # 模拟处理时间

server_address = ('', 5000)
httpd = HTTPServer(server_address, StreamingHandler)
print("Starting server at http://localhost:5000/")
httpd.serve_forever()